package com.bojiu.lawsearch.lawsearch.service.impl;

import com.bojiu.lawsearch.lawsearch.mapper.*;
import com.bojiu.lawsearch.lawsearch.model.*;
import com.bojiu.lawsearch.lawsearch.service.BackUserService;
import com.bojiu.lawsearch.lawsearch.util.UtilTools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: afdiao
 * @Date：2020/3/8 18:34
 * @Description:
 */
@Service
@Slf4j
public class BackUserServiceImpl implements BackUserService {

    @Autowired
    WechatUserMapper wechatUserMapper;

    @Autowired
    EngineerMapper engineerMapper;

    @Autowired
    LabelMapper labelMapper;

    @Autowired
    UserAccountMapper userAccountMapper;

    @Autowired
    EngineerLabelMapper engineerLabelMapper;

    @Override
    public Page<WechatUser> getUserList(String key, String type, String province, String city, String label_id, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<WechatUser> wechatUsers = new ArrayList<>();
        if ("1".equals(type)) {
            wechatUsers.addAll(wechatUserMapper.getListByEngineer(key, province, city, label_id, "1"));
        } else {
            wechatUsers.addAll(wechatUserMapper.getUserList(key, province, city, type));
        }
        for (WechatUser wechatUser : wechatUsers) {
            //获取工程师
            Map<String, Object> map = new HashMap<>();
            map.put("user_id", wechatUser.getId());
            map.put("is_authentication", 1);
            if (engineerMapper.selectCountByExample(UtilTools.createExampleByMap(map, Engineer.class)) > 0) {
                Engineer engineer = (Engineer) engineerMapper.selectOneByExample(UtilTools.createExampleByMap(map, Engineer.class));
                wechatUser.setEngineer(engineer);
            }
            //获取积分余额
            if (userAccountMapper.selectCountByExample(UtilTools.createExampleByKey("user_id", wechatUser.getId(), UserAccount.class)) > 0) {
                UserAccount userAccount = (UserAccount) userAccountMapper.selectOneByExample(UtilTools.createExampleByKey("user_id", wechatUser.getId(), UserAccount.class));
                wechatUser.setUserAccount(userAccount);
            } else {
                UserAccount userAccount = new UserAccount();
                userAccount.setIntegral(0);
                userAccount.setAccount(0);
                wechatUser.setUserAccount(userAccount);
            }
            log.debug("user is :{}", wechatUser);
        }
        PageInfo pageInfo = new PageInfo(wechatUsers);
        Page page = new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), pageInfo.getTotal(), wechatUsers);
        return page;
    }

    @Override
    public void deleteById(int id) {
        WechatUser wechatUser = new WechatUser();
        wechatUser.setIs_deleted(1);
        wechatUserMapper.updateByExampleSelective(wechatUser, UtilTools.createExampleByKey("id", id, WechatUser.class));
    }

    @Override
    public Page<WechatUser> getEngineerList(int pageNum, int pageSize, String state) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<WechatUser> listByEngineer = wechatUserMapper.getListByEngineer(null, null, null, null, state);
        //添加标签名
        for (WechatUser wechatUser : listByEngineer) {
            Map<String, Object> map = new HashMap<>();
            map.put("user_id", wechatUser.getId());
            map.put("is_authentication", 0);
            Engineer engineer = (Engineer) engineerMapper.selectOneByExample(UtilTools.createExampleByMap(map, Engineer.class));
            String sql = UtilTools.changeStrForSql(engineer.getLabel_id());
            engineer.setLabelNames(labelMapper.getLabelNameInIds(sql));
            wechatUser.setEngineer(engineer);
        }
        PageInfo pageInfo = new PageInfo(listByEngineer);
        Page page = new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), pageInfo.getTotal(), listByEngineer);
        return page;
    }

    @Transactional
    @Override
    public void handleEngineer(int id, int state, String reason) {
        Engineer engineer = new Engineer();
        engineer.setIs_authentication(state);
        engineer.setReason(reason);
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", id);
        map.put("is_authentication", 0);
        engineerMapper.updateByExampleSelective(engineer, UtilTools.createExampleByMap(map, Engineer.class));
        if (2 == state) {
            //不同意就删除标签
            engineerLabelMapper.deleteByExample(UtilTools.createExampleByKey("userId", id, EngineerLabel.class));
        }
        if (1 == state) {
            WechatUser wechatUser = new WechatUser();
            wechatUser.setType(1);
            wechatUserMapper.updateByExampleSelective(wechatUser, UtilTools.createExampleByKey("id", id, WechatUser.class));
        }
    }
}
